var isIdCard1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/,
    isIdCard2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}[A-Za-z0-9]{1}$/;//身份证验证正则
var regex = /^(13|14|15|18|17)[0-9]{9}$/;//手机号码
var regExp = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; // 密码
var email_reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/; // email
var stringReg = /^[A-Za-z0-9]+$/;
var bank_reg = /^(\d{16}|\d{17}|\d{18}|\d{19})$/;  //银行卡号
var minSexNumber = /^\d{6,}$/;
var publicBank = /^\d{10,24}$/;

var checkPhone = function (v) {
    v = v || $('#phone').val();
    if (!regex.test(v)) {
        wjtips("手机号格式不正确，请核对后重新填写");
        return false;
    } else {
        return true;
    }
};

var checkUsername = function (v) {
    v = v || $('#phone').val();
    if (v == '') {
        wjtips("手机/用户名不能为空！");
        return false;
    } else {
        return true;
    }
};

var checkPassword = function (v) {
    v = v || $('#password').val();
    if (v == '') {
        wjtips("密码不能为空，请输入8至16位英文字母+数字组合！");
        return false;
    }
    if (!regExp.test(v)) {
        wjtips("密码格式错误，请输入8至16位英文字母+数字组合！");
        return false;
    }
    return true;
};


var checkoldPassword = function (v) {
    v = v || $('#password').val();
    if (v == '') {
        wjtips("原始密码不能为空！");
        return false;
    }
    return true;
};

var recheckPassword = function (v1, v2) {

    v1 = v1 || $('#password1').val();
    v2 = v2 || $('#password2').val();
    if (v1 == '') {
        wjtips("新密码不能为空，请输入8至16位英文字母+数字组合！");
        return false;
    }
    if (!regExp.test(v1)) {
        wjtips("密码格式错误，请输入8至16位英文字母+数字组合！");
        return false;
    }

    if (v1 != v2) {
        wjtips("两次密码输入不一致");
        return false;
    }
    return true;
};

var checkLoginPassword = function (v) {
    v = v || $('#password').val();
    if (v == '') {
        wjtips("密码不能为空！");
        return false;
    }
    return true;
};

//检验手机验证码
var checkPhonecode = function (v) {
    v = v || $('#scode').val();
    if (v.length != 6) {
        wjtips("请输入6位短信验证码");
        return false;
    } else {
        return true;
    }
};

var checkVerify = function (v) {
    v = v || $('#verify').val();
    if (v.length != 4) {
        wjtips('请输入4位图形验证码');
        return false;
    }
    return true;
};

var checkRealname = function (v) {
    v = v || $('#real_name').val();
    var real_name_test = /^[\u4E00-\u9FA5A-Za-z]+$/;
    if (!real_name_test.test(v)) {
        wjtips("请输入正确的姓名");
        return false;
    }
    return true;
};

var checkEmail = function () {
    var id = "#email";
    var email = $(id).val();
    if (!email_reg.test(email)) {
        wjtips("请输入正确的email格式!");
        return false;
    }
    return true;
};

var checkIdcard = function (v) {
    v = v || $('#idcard').val();
    if (!isIdCard1.test(v) && !isIdCard2.test(v)) {
        wjtips("身份证号码格式不正确，请重新输入！");
        return false;
    }
    return true;
};


var checkBankcard = function (v) {
    v = v || $('#card_id').val().replace(/\s/g, '');
    if (v == '') {
        wjtips('请先输入银行卡号');
        return;
    } else if (isNaN(v) == true) {
        wjtips("银行卡号只能为数字！");
        return;
    } else if (v.length >= 16 && v.length <= 19) {
        return true;
    } else {
        wjtips("银行卡号格式错误,请检查位数是否正确");
        return false;
    }

    return true;
};

var checkPublicBank = function (v) {
    v = v || $('#publicBank').val().replace(/\s/g, '');
    if (!publicBank.test(v)) {
        wjtips("请输入正确的绑定银行卡号！");
        return false;
    }
    return true;
};

var checkBankcardType = function () {
    if ($('#bank_card_type').val() == 1) {
        wjtips("请填写储蓄卡进行绑定！");
        return false;
    }
    return true;
};

var isSendbtm = function (v) {
    if (!checkPhone()) {
        return false;
    }
    if (!checkVerify()) {
        return false;
    }
    sendMobileValidSMSCode();
};

var refreshEcode = function () {
    var e = $('.verifyimg');
    var src = e.attr('src');
    e.attr("src", src.replace(/\?.*$/, '') + '?' + Math.random());
    $('#verify').val('');
};

//发送短信操作
var timer = null;
var leftsecond = 60; //倒计时
function sendMobileValidSMSCode(sType) {
    if (leftsecond !== 60) {
        return false;
    }
    var verify = $("#verify").val();
    var phone = $("#phone").val();
    var sendcodeurl = $('#sendcodeurl').val();
    var type = $('#type').val() == 1 ? $('#type').val() : 0;

    wjtips("短信发送中...");
    $("#btnSendMsg").removeAttr('onclick');

    $.ajax({
        url: sendcodeurl,
        type: "post",
        dataType: "json",
        data: {
            phone: phone,
            verify: verify,
            type: type
        },
        success: function (d) {
            leftsecond = 60;
            if (d.code == 1) {
                wjtips(d.msg);
                clearInterval(timer);
                timer = setInterval(setLeftTime, 1000, "1");
                $("#btnSendMsg").val("请稍等..").addClass('dis');
                $("#phone").attr("readonly", true);

            } else {
                $("#btnSendMsg").html("获取验证码");
                $("#phone").attr("readonly", false);
                $("#btnSendMsg").attr({
                    onclick: 'isSendbtm()'
                });
                if (d.msg) {
                    wjtips(d.msg);
                    return false;
                }
                wjtips(checkErrorType(d));
                if (sType !== 99) {
                    refreshEcode();
                }
            }
        }
    });
}


//  连续错误提示
function checkErrorType(d) {
    if (d.code == 1) return true;
    for (var p in d.data) {
        if (p == 'phone') return d.data[p];
        if (p == 'verify') return d.data[p]
        if (p == 'scode') return d.data[p];
        if (p == 'password') return d.data[p];
        if (p == 'password1') return d.data[p];
        if (p == 'password2') return d.data[p];
        if (p == 'exp_phone') return d.data[p];
    }
}

//刷新重新获取时间
function setLeftTime() {
    var second = Math.floor(leftsecond);
    $("#btnSendMsg").html(second + "s后重新发送");
    leftsecond--;
    if (leftsecond < 1) {
        clearInterval(timer);
        leftsecond = 60;
        try {
            $("#btnSendMsg").html("获取验证码").removeClass('dis');
            $("#mobile").removeAttr("readonly");
            $("#btnSendMsg").attr({
                onclick: 'isSendbtm()',
            });
        } catch (E) {
        }
        return;
    }
}


function checkingMoney(value) {
    if (value >= 1) {
        var index = value.indexOf(".");
        if (index == 0) {
            return "";
        }
        if (index != -1) {
            var arr = value.split(".");
            var h = "", b = "";
            for (var i = 0; i < arr.length; i++) {
                if (i == 0) {
                    h = arr[i].replace(/\D/g, '') + ".";
                } else {
                    b += "" + arr[i].replace(/\D/g, '');
                    if (b.length >= 2) {
                        break;
                    }
                }
            }
            if (h.charAt(0) == "0") {
                h = "0.";
            }
            value = h + b.substr(0, 2);
        } else {
            value = value.replace(/\D/g, '');
            if (value.charAt(0) == "0") {
                value = parseInt(value, 10);
            }
        }
        return value == "0.00" ? "0" : value;
    }
    else {
        return value = "";
    }
}

var checkWithdrawMoney = function (v) {
    v = v || $('#money').val();
    var withmaxmoney = parseFloat($('#account_money').text());
    var withminmoney = 100;

    if (v > withmaxmoney) {
        wjtips('提现金额不能大于可提现余额' + withmaxmoney + ' 元');
        return false;
    }

    if (v < 100) {
        wjtips('提现金额不能小于' + withminmoney + ' 元');
        return false;
    }

    return true;
};

var checkBankarea = function (v) {
    var v = v || $('#area').val();
    if (v == '') {
        wjtips('请选择开户行所在地区');
        return false;
    }

    var vArr = v.split(' ');
    $('#province').val(vArr[0]);
    $('#city').val(vArr[1]);
    $('#district').val(vArr[2]);

    return true;
};

var checkBankaddress = function (v) {
    var v = v || $('#card_address').val();
    var adressreg = new RegExp("^[\u4e00-\u9fa5]{2,20}$");
    var m = adressreg.test(v); //支行名称为2-20位中文字符
    var m2 = v.slice(v.length - 2, v.length); // 支行截取 最后两个字符
    if (!m) {
        wjtips("请输入2-20个字符中文开户支行名称");
        return false;
    }
    if (!(m2 == "支行" || m2 == "分行")) {
        wjtips("支行名称必须以“支行”或“分行”结尾");
        return false;
    }
    return true;
};

$(function () {
    $('.verifyimg').click(function () {
        refreshEcode();
    });
    $('.passeye').click(function () {
        var passinput = $(this).parent().find('input');
        if ($(this).parent().find('input').attr('type') == 'password') {
            passinput.prop('type', 'text');
            $(this).html('&#xe625;');
        } else {
            passinput.prop('type', 'password');
            $(this).html('&#xe626;');
        }
    })
})